/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.security.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.zxp.common.controller.BaseController;
import com.zxp.common.core.constant.module.SecurityConstants;
import com.zxp.common.core.validatecode.impl.ImageValidateCodeService;
import com.zxp.common.utils.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: shollin
 * @date: 2021/7/3/003 16:46
 */
@Api("登陆管理api")
@Controller
@Slf4j
@RequiredArgsConstructor
public class LoginController extends BaseController {

    private final RedisTemplate<String,Object> redisTemplate;
    private final ImageValidateCodeService imageValidateCodeService;

    @ApiOperation(value="账号密码登录", notes="账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "账号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })

    /**
     * 前后端分享的项目，不会进来这个url
    */
    @GetMapping("/login")
    public String loginUI(HttpServletRequest request,HttpServletResponse response){
        String cookieVal = WebUtil.getCookieVal(SecurityConstants.CAPTCHA_COOKIENAME_UUID);
        if(StrUtil.isBlank(cookieVal)){
            // 验证码的uuid身份，可以保存久些，为一年
            WebUtil.setCookie(response, SecurityConstants.CAPTCHA_COOKIENAME_UUID,UUID.randomUUID().toString(),360*24*3600);
        }
        return view("login/login");
    }

    @ApiOperation(value="账号密码登录", notes="账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "账号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    @GetMapping("/reg")
    public String reg(HttpServletRequest request, HttpServletResponse response, Model model){

    	return "reg";
    }

    @ApiOperation("hello信息")
    @GetMapping("/")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}
